package com.example.dps;


import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class HistoryHelper extends SQLiteOpenHelper {
	
	
	
	/**********************************************************************
	 * Database Variables
	 */
    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "DownPaymentSaver.db";
    /********************************************************************/
    
    
    /**********************************************************************
     * TABLE VARIABLES
     */
    
    public static final String TABLE_NAME = "History";
    
    public static final String COLUMN_USERNAME = "username";
    public static final String COLUMN_FROM = "fromAccount";
    public static final String COLUMN_TO = "toAccount";
    public static final String COLUMN_AMOUNT = "amount";
    public static final String COLUMN_DATE = "date";
    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_STATUS = "status";
    
    public static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" +
    		COLUMN_ID + " INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, " +
    		COLUMN_USERNAME + " VARCHAR(50), " +
    		COLUMN_FROM + " VARCHAR(50), " +
    		COLUMN_TO + " VARCHAR(50), " +
    		COLUMN_AMOUNT + " VARCHAR(20), " +
    		COLUMN_DATE + " VARCHAR(8), " +
    		COLUMN_STATUS + " VARCHAR(15)," +
    		"FOREIGN KEY (" + COLUMN_FROM + ") REFERENCES " +	AccountDBHelper.ACCOUNT_TABLE_NAME + "(" + AccountDBHelper.COLUMN_ACCOUNT_NAME + "),"	+  //TODO how to store FROM/TO accounts? checking num? reference to ACCOUNTS?
    		"FOREIGN KEY (" + COLUMN_TO + ") REFERENCES " +	AccountDBHelper.ACCOUNT_TABLE_NAME + "(" + AccountDBHelper.COLUMN_ACCOUNT_NAME + ")"	+ 
    		");";
    		
    /********************************************************************/
    
    
    private SQLiteDatabase db;
    

	public HistoryHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		
		this.db = getWritableDatabase();
		
		this.db.execSQL(CREATE_TABLE);
		
		this.db.close();
		
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		
	}
	
	
	public Cursor getHistoryAsCursor(){
		
		this.db = getReadableDatabase();
		
		String query = "SELECT * FROM History";
		
		Cursor result = this.db.rawQuery(query, null);
		
		return result;
		
	}
	
	public void addTransfer(String from, String to, int amount){
		
		this.db = getWritableDatabase();
		
		Log.e("HISTORYHELPER", "adding Transfer !!!!");

		String date = new SimpleDateFormat("MM/dd/yyyy").format(new Date());

		ContentValues values = new ContentValues();
		
		String strAmount = new DecimalFormat("$#,###").format(amount);
		
		values.put(COLUMN_FROM, from);
		values.put(COLUMN_TO, to);
		values.put(COLUMN_AMOUNT, strAmount);
		values.put(COLUMN_DATE, date);
		
		this.db.insert(TABLE_NAME, null, values);

		this.db.close();
		
	}
	
	public void clearHistory(){
		this.db = getWritableDatabase();
		
		String query = "Delete From " + TABLE_NAME;
		
		this.db.execSQL(query);
		
		this.db.close();
		
	}
	

}
